package com.hazam.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

public class GraphicUtils {
	
	/**
	 * Creates rounded bitmap
	 * @param bitmap the source bitmap
	 * @return the rounded bitmap
	 */
	public static Bitmap createRoundedBitmap(Bitmap bitmap, int round) {
	    if (bitmap == null) {
	        return null;
	    }
	        
	    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

	    // create output bitmap
	    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

	    // assign canvas with output bitmap
	    Canvas canvas = new Canvas(output);
	    canvas.drawARGB(0, 0, 0, 0);
	        
	    // initialize paint
	    Paint paint = new Paint();
	    paint.setAntiAlias(true);

	    // draw rounded rect to bitmap
	    paint.setColor(0xFFFFFFFF);
	    canvas.drawRoundRect(new RectF(rect), round, round, paint);

	    // copy original bitmap to rounded area
	    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	    canvas.drawBitmap(bitmap, rect, rect, paint);
	        
	    return output;
	}
}
